<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div v-show="flag" class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="mini" :inline="true" class="demo-form-inline">
        <el-form-item label="广告位名称">
          <el-input v-model="qaueryData.advposname" placeholder="请输入广告位名称" />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="open('add')">新增</el-button>
        </div>

        <!-- 弹框 -->
        <el-dialog
          :title="title"
          :visible.sync="dialogVisible"
          width="40%"
          :before-close="handleClose"
        >

          <el-form ref="form" :model="form" status-icon :rules="rules" label-width="100px" class="demo-ruleForm" style="width:100%">
            <el-form-item label="广告位描述" prop="advposname">
              <el-input v-model="form.advposname" type="text" autocomplete="off" placeholder="请输入广告位名称" />
            </el-form-item>
            <el-form-item label="广告位描述" prop="advposdesc">
              <el-input v-model="form.advposdesc" type="textarea" placeholder="请输入广告位描述" />
            </el-form-item>
            <el-form-item label="广告位尺寸" prop="advpossize">
              <el-input v-model="form.advpossize" autocomplete="off" placeholder="请输入广告位尺寸" />
            </el-form-item>

            <el-form-item>
              <el-button @click="cancel">取 消</el-button>
              <el-button type="primary" @click="submit">确 定</el-button>
            </el-form-item>
          </el-form>

        </el-dialog>

        <div>
          <el-button v-if="flag" icon="el-icon-arrow-up" circle size="mini" @click="flag=!flag" />
          <el-button v-else icon="el-icon-arrow-down" circle size="mini" @click="flag=!flag" />
          <el-button icon="el-icon-refresh" circle size="mini" @click="initDate()" />
          <el-button icon="el-icon-menu" circle size="mini" />
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table
        :data="list"
        style="width: 100%"
        border
      >
        <el-table-column prop="index" label="#" width="50" />
        <el-table-column prop="advposname" label="广告位名称" />
        <el-table-column prop="advposdesc" label="广告位描述" />
        <el-table-column prop="advposnum" label="广告图数量" />
        <el-table-column prop="advpossize" label="广告位尺寸" />
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button type="primary" icon="el-icon-edit" circle @click="open('edit',row.id)" />
            <el-button type="danger" icon="el-icon-delete" circle @click="del(row.id)" />
          </template>

        </el-table-column>
      </el-table>
    </div>
    <!-- 分页区域 -->
    <div class="page">
      <el-pagination
        :current-page="qaueryData.page"
        :page-sizes="[1,2,5, 10, 20, 30]"
        :page-size="qaueryData.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

  </div>
</template>
<script>
import { getPos, addPos, editPos, updataPos, delPos } from '@/api/adv/pos'
export default {
  data() {
    return {
      titleType: '', // 接受弹框类型
      dialogVisible: false, // 控制弹框开关
      title: '', // 弹框标题
      flag: true,
      list: [],
      qaueryData: {
        advposdesc: '',
        advposname: '',
        page: 1,
        limit: 10
      },
      total: 0,
      form: {
        advposname: '',
        advposdesc: '',
        advpossize: ''

      },
      id: '',
      rules: {
        advposname: [{ required: true, message: '广告位名称不能为空', trigger: 'blur' }],
        advposdesc: [{ required: true, message: '广告位描述不能为空', trigger: 'blur' }],
        advpossize: [{ required: true, message: '广告位尺寸不能为空', trigger: 'blur' }]
      }

    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 删除
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPos({ id })
        // console.log(res)
        if (this.list.length <= 1) {
          this.params.page -= 1
        }
        this.initData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 取消并关闭弹窗
    cancel() {
      this.handleClose()
    },
    // 实现添加和修改
    async submit() {
      // 添加功能
      if (this.titleType === 'add') {
        this.$refs.form.validate(isOk => {
          if (!isOk) return
        })
        try {
          await addPos(this.form)
          // console.log(res)
          this.handleClose()
          this.initData()
        } catch (error) {
          console.log(error)
        }
      }

      // 修改功能
      if (this.titleType === 'edit') {
        await updataPos({
          advposdesc: this.form.advposdesc,
          advposname: this.form.advposname,
          advpossize: this.form.advpossize,
          default_data: 0,
          id: this.id
        })
        this.handleClose()
        this.initData()
      }
    },
    // 判断弹窗的状态和编辑数据回显
    async open(type, id) {
      this.dialogVisible = true
      this.titleType = type
      if (this.titleType === 'add') {
        this.title = '添加广告位'
      }
      if (this.titleType === 'edit') {
        this.title = '修改广告位'
        this.id = id
        // 修改的时候数据回显
        const res = await editPos({ id })
        // console.log(res)
        this.form = res.data
      }
    },
    // 弹框的回调函数
    handleClose() {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    // 更改每一页的条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.qaueryData.limit = val
      this.qaueryData.page = 1
      this.initData()
    },
    // 更改页数
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.qaueryData.page = val
      this.initData()
    },
    // 重置
    reset() {
      this.qaueryData = {
        advposdesc: '',
        advposname: '',
        page: 1,
        limit: 10
      }
      this.initData()
    },
    // 搜索
    search() {
      this.qaueryData.page = 1
      this.initData()
    },
    // 渲染
    async initData() {
      const res = await getPos(this.qaueryData)
      // console.log(res)
      this.list = res.data.data
      this.total = res.data.count
    }
  }
}
</script>
<style lang="scss" scoped>
  .page-box {
    padding: 20px;
    .search-box,.tabbar-box {
      padding-top: 10px;
      border:  1px solid #eee;
      margin-bottom: 20px;
      height: 50px;
    }
  }

</style>
